/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RegisterUser.java
 *
 * Created on Mar 21, 2011, 12:59:56 PM
 */

/**
 *
 * @author Eduardo
 */
public class Input extends javax.swing.JFrame {


    /** Creates new form RegisterUser */
    public Input() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        UIDLabel = new javax.swing.JLabel();
        ConfirmButton = new javax.swing.JButton();
        InfoLabel = new javax.swing.JLabel();
        TitleLabel = new javax.swing.JLabel();
        ClearButton = new javax.swing.JButton();
        PasswConfirmLabel = new javax.swing.JLabel();
        FirstNameLabel = new javax.swing.JLabel();
        PasswLabel = new javax.swing.JLabel();
        LastNameLabel = new javax.swing.JLabel();
        EmailLabel = new javax.swing.JLabel();
        PhoneLabel = new javax.swing.JLabel();
        FirstNameText = new javax.swing.JTextField();
        LastNameText = new javax.swing.JTextField();
        EmailText = new javax.swing.JTextField();
        Phone0Text = new javax.swing.JTextField();
        UIDText = new javax.swing.JTextField();
        PasswText = new javax.swing.JPasswordField();
        PassConfirmText = new javax.swing.JPasswordField();
        FirstNameOut = new javax.swing.JLabel();
        LastNameOut = new javax.swing.JLabel();
        EmailOut = new javax.swing.JLabel();
        PhoneOut = new javax.swing.JLabel();
        UIDOut = new javax.swing.JLabel();
        PassOut = new javax.swing.JLabel();
        Phone1Text = new javax.swing.JTextField();
        Phone2Text = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));
        setBounds(new java.awt.Rectangle(50, 30, 800, 600));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setForeground(java.awt.Color.white);
        setMinimumSize(new java.awt.Dimension(800, 600));
        setName("RegisterFrame"); // NOI18N
        setResizable(false);

        UIDLabel.setText("User ID:");

        ConfirmButton.setText("Register & Continue");
        ConfirmButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ConfirmButtonActionPerformed(evt);
            }
        });

        InfoLabel.setText("Please provide the following information. All fields are required.");

        TitleLabel.setFont(new java.awt.Font("Tahoma", 0, 48)); // NOI18N
        TitleLabel.setText("Register New User");

        ClearButton.setText("Clear Form");
        ClearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ClearButtonActionPerformed(evt);
            }
        });

        PasswConfirmLabel.setText("Confirm Password:");

        FirstNameLabel.setText("First Name:");

        PasswLabel.setText("Password:");

        LastNameLabel.setText("Last Name:");

        EmailLabel.setText("e-mail:");

        PhoneLabel.setText("Phone:");

        FirstNameOut.setText("                                         ");

        LastNameOut.setText("                                         ");

        EmailOut.setText("                                         ");

        PhoneOut.setText("                                         ");

        UIDOut.setText("                                         ");

        PassOut.setText("                                         ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(PasswLabel)
                            .addComponent(PhoneLabel)
                            .addComponent(LastNameLabel)
                            .addComponent(UIDLabel)
                            .addComponent(EmailLabel)
                            .addComponent(FirstNameLabel)
                            .addComponent(PasswConfirmLabel))
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(Phone0Text, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(26, 26, 26)
                                    .addComponent(Phone1Text, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                                    .addComponent(Phone2Text, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(EmailText, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                                .addComponent(UIDText, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                                .addComponent(PasswText, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                                .addComponent(PassConfirmText, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                                .addComponent(LastNameText, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE))
                            .addComponent(FirstNameText, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(17, 17, 17))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(ConfirmButton)
                        .addGap(98, 98, 98)
                        .addComponent(ClearButton)))
                .addGap(97, 97, 97)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(LastNameOut, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(EmailOut, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(PhoneOut, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(UIDOut, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(PassOut, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(FirstNameOut, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(65, 65, 65))
            .addGroup(layout.createSequentialGroup()
                .addGap(221, 221, 221)
                .addComponent(InfoLabel)
                .addContainerGap(276, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(194, 194, 194)
                .addComponent(TitleLabel)
                .addContainerGap(217, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(TitleLabel)
                .addGap(25, 25, 25)
                .addComponent(InfoLabel)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(25, 25, 25)
                                .addComponent(LastNameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(EmailText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(Phone0Text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(Phone2Text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(Phone1Text, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addComponent(UIDText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(PasswText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(PassConfirmText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(FirstNameLabel)
                                .addGap(18, 18, 18)
                                .addComponent(LastNameLabel)
                                .addGap(18, 18, 18)
                                .addComponent(EmailLabel)
                                .addGap(24, 24, 24)
                                .addComponent(PhoneLabel)
                                .addGap(24, 24, 24)
                                .addComponent(UIDLabel)
                                .addGap(24, 24, 24)
                                .addComponent(PasswLabel)
                                .addGap(29, 29, 29)
                                .addComponent(PasswConfirmLabel)))
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(ClearButton)
                            .addComponent(ConfirmButton)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(FirstNameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(FirstNameOut)
                        .addGap(18, 18, 18)
                        .addComponent(LastNameOut)
                        .addGap(18, 18, 18)
                        .addComponent(EmailOut)
                        .addGap(18, 18, 18)
                        .addComponent(PhoneOut)
                        .addGap(18, 18, 18)
                        .addComponent(UIDOut)
                        .addGap(18, 18, 18)
                        .addComponent(PassOut)))
                .addContainerGap(132, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ConfirmButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConfirmButtonActionPerformed
        // TODO add your handling code here:
        if (EmailText.getText().length() == 0) {
            EmailOut.setText("Email Field Required");
        } else {
            EmailOut.setText("");
            ValidateEmail();
        }

        if (LastNameText.getText().length() == 0) {
            LastNameOut.setText("Last Name Field Required");
        } else {
            LastNameOut.setText("");
        }

        if (FirstNameText.getText().length() == 0) {
            FirstNameOut.setText("First Name Field Required");
        } else {
            FirstNameOut.setText("");
        }

        if (UIDText.getText().length() == 0) {
            UIDOut.setText("User ID Field Required");
        } else {
            UIDOut.setText("");
        }

        if (Phone0Text.getText().length() == 0 || Phone1Text.getText().length() == 0 || Phone2Text.getText().length() == 0) {
            PhoneOut.setText("Phone Field Required");
        } else {
            PhoneOut.setText("");
            ValidatePhone();
        }

        if (PasswText.getText().length() == 0) {
            PassOut.setText("Password Field Required");
        } else {
            PassOut.setText("");
            ValidatePassword();
        }



    }//GEN-LAST:event_ConfirmButtonActionPerformed

    private void ClearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClearButtonActionPerformed
        // TODO add your handling code here:
        EmailText.setText("");
        FirstNameText.setText("");
        LastNameText.setText("");
        PasswText.setText("");
        PassConfirmText.setText("");
        Phone0Text.setText("");
        UIDText.setText("");
        EmailOut.setText("");
        FirstNameOut.setText("");
        LastNameOut.setText("");
        PassOut.setText("");
        PhoneOut.setText("");
        UIDOut.setText("");
        Phone1Text.setText("");
        Phone2Text.setText("");

    }//GEN-LAST:event_ClearButtonActionPerformed

    private void ValidatePassword() {

        if (PasswText.getText().length() >= 6) {
            PassOut.setText("");
           if (PasswText.getText().equals(PassConfirmText.getText()) == false ) {
               PassOut.setText("Passwords do not match");
           } else {
              PassOut.setText("");  
           }
        } else {
            PassOut.setText("Password must be at least 6 characters");
        }
    }

    private void ValidateEmail() {
        char[] temp1 = EmailText.getText().toCharArray();
        String temp2 = EmailText.getText().toString();
        int count = 0;
        int size = temp1.length;
        int ascii1 = 0;
        int ascii2 = 0;
        int ascii3 = 0;
        int ascii4 = 0;
        int LastDot = temp2.lastIndexOf(".");
        //Determine if ends something like .com or .ca
        int diff = size - LastDot;

        
        try {
            ascii1 = EmailText.getText().toLowerCase().charAt(size-1);
            ascii2 = EmailText.getText().toLowerCase().charAt(size-2);
            ascii3 = EmailText.getText().toLowerCase().charAt(size-3);
            ascii4 = EmailText.getText().toLowerCase().charAt(size-4);

        } catch (StringIndexOutOfBoundsException siobe) {}

        for (int i = 0; i < temp1.length; i++) {
            if (temp1[i] == '@') {
                count += 1;
            }
        }
        if (diff == 3) {
            if (ascii1 < 97 || ascii1 > 122) {
                EmailOut.setText("Invalid Email Address");
            } else if (ascii2 < 97 || ascii2 > 122) {
                EmailOut.setText("Invalid Email Address");
            }
        } else if (diff == 4) {
            if (ascii1 < 97 || ascii1 > 122) {
                EmailOut.setText("Invalid Email Address");
            } else if (ascii2 < 97 || ascii2 > 122) {
                EmailOut.setText("Invalid Email Address");
            } else if (ascii3 < 97 || ascii3 > 122) {
                EmailOut.setText("Invalid Email Address");
            } 
        } else {
            EmailOut.setText("Invalid Email Address");
        }
        if (count != 1) {
            EmailOut.setText("Invalid Email Address");
        }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Input().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ClearButton;
    private javax.swing.JButton ConfirmButton;
    private javax.swing.JLabel EmailLabel;
    private javax.swing.JLabel EmailOut;
    private javax.swing.JTextField EmailText;
    private javax.swing.JLabel FirstNameLabel;
    private javax.swing.JLabel FirstNameOut;
    private javax.swing.JTextField FirstNameText;
    private javax.swing.JLabel InfoLabel;
    private javax.swing.JLabel LastNameLabel;
    private javax.swing.JLabel LastNameOut;
    private javax.swing.JTextField LastNameText;
    private javax.swing.JPasswordField PassConfirmText;
    private javax.swing.JLabel PassOut;
    private javax.swing.JLabel PasswConfirmLabel;
    private javax.swing.JLabel PasswLabel;
    private javax.swing.JPasswordField PasswText;
    private javax.swing.JTextField Phone0Text;
    private javax.swing.JTextField Phone1Text;
    private javax.swing.JTextField Phone2Text;
    private javax.swing.JLabel PhoneLabel;
    private javax.swing.JLabel PhoneOut;
    private javax.swing.JLabel TitleLabel;
    private javax.swing.JLabel UIDLabel;
    private javax.swing.JLabel UIDOut;
    private javax.swing.JTextField UIDText;
    // End of variables declaration//GEN-END:variables

    private void ValidatePhone() {
        
         int AreaCode = 0;
         int Prefix = 0;
         int Suffix = 0;
        
        try {

            if (Phone0Text.getText().length() != 3) {
                PhoneOut.setText("Invalid Phone Number");
            } else if (Phone1Text.getText().length() != 3) {
                PhoneOut.setText("Invalid Phone Number");
            } else if (Phone2Text.getText().length() != 4) {
                PhoneOut.setText("Invalid Phone Number");
            } else {
                AreaCode = Integer.parseInt(Phone0Text.getText().toString());
                Prefix = Integer.parseInt(Phone1Text.getText().toString());
                Suffix = Integer.parseInt(Phone2Text.getText().toString());
                PhoneOut.setText("");
            }
        } catch (NumberFormatException nfe) {
            PhoneOut.setText("Invalid Phone Number");
        }
    }

}
